﻿using System.Windows;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Regions.Behaviors;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using UnityExtensions;

namespace Sample
{
    public class Bootstrapper : UnityBootstrapper
    {
        protected override IModuleCatalog CreateModuleCatalog()
        {
            return new ConfigurationModuleCatalog();
        }

        protected override DependencyObject CreateShell()
        {
            return new MainShell();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();
            Application.Current.MainWindow = (Window)Shell;
            Application.Current.MainWindow.Show();
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            Container
                .RegisterType<IServiceLocator, UnityServiceLocator>(new HierarchicalLifetimeManager())
                .RegisterType<IScopedRegionViewRegistry, ScopedRegionViewRegistry>(new ContainerControlledLifetimeManager());
        }

        protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
        {
            Container.Resolve<IRegionBehaviorFactory>()
                .AddIfMissing(AutoPopulateRegionBehavior.BehaviorKey, typeof(ScopedAutoPopulateRegionBehavior));
            return base.ConfigureDefaultRegionBehaviors();
        }
    }
}
